﻿using liaowa2.Entity.DTO;
using liaowa2.Entity.InputDTO;
using liaowa2.Interface.Manager;
using liaowa2.Utils;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace liaowa2.Web.Controllers
{
    /// <summary>
    /// 用户管理  yaols
    /// </summary>
    public class ManagerUserController : BaseController
    {

        private IManagerUserInterface _ManagerUser;
        private IManagerRoleInterface _managerRole;

        public ManagerUserController(IManagerUserInterface ManagerUser, IManagerRoleInterface managerRole)
        {
            this._ManagerUser = ManagerUser;
            this._managerRole = managerRole;
        }


        // GET: ManagerUser
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult LoadData(ManagerUserView input)
        {
            var admin = GetAdmin();
            input.RoleId = admin.role_id;
            var output = _ManagerUser.GetManagerUserPage(input);

            var totalPage = (output.Count + input.PageSize - 1) / input.PageSize; //总页数
            return Json(new { List = output.Items, Count = totalPage, Total = output.Count }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 启动或禁用
        /// </summary>
        /// <param name="id"></param>
        /// <param name="enable"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult IsEnable(int id, int enable)
        {
            var output = _ManagerUser.IsEnable(id, enable);
            return Json(new { Success = output });
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> Edit(int id=0)
        {

            ManagerUser managerUser = new ManagerUser();

            ViewBag.Role = _managerRole.GetAllList();
            if (id > 0)
            {
                managerUser = await _ManagerUser.GetModel(id);
            }

            return View(managerUser);

        }

        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {

            ManagerUser model = new ManagerUser();
            model.id= Convert.ToInt32(collection["id"]);
            model.user_name = collection["user_name"];
            model.password = EncodeSecurity.EncryptString(collection["password"]);
            model.real_name= collection["real_name"];
            model.role_id=Convert.ToInt32(collection["ddlRole"]);
            model.telephone= collection["telephone"];
            model.user_code = Guid.NewGuid().ToString();
            model.email= collection["email"];

            if (!_ManagerUser.CheckUserName(model.user_name))
            {
                AlertMessage("用户名已存在！");
                return null;
            }

            if (Convert.ToInt32(collection["id"]) > 0)
            {
                _ManagerUser.EditUser(model);
                return AlertByUrl("操作成功！", "/ManagerUser/Index");
            }
            else
            {
                _ManagerUser.AddUser(model);
                return AlertByUrl("操作成功！", "/ManagerUser/Index");
            }
        }

    }
}